package com.sino.uam.support;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sino.uam.rbac.AbstractUAMRequest;
import com.sino.uam.support.cache.DepartmentFactory;
import com.sino.uam.support.cache.RoleFactory;
import com.sino.uam.support.cache.UserInfoFactory;

public class UAMFascade {
    protected static Logger logger = Logger.getLogger(UAMFascade.class);
    private static Map<String, Object> map = new HashMap<String, Object>();
    static {
        UserInfoFactory factory = new UserInfoFactory();
        DepartmentFactory dpt = new DepartmentFactory();
        RoleFactory role = new RoleFactory();
        map.put("UserInfoFactory", factory);
        map.put("DepartmentFactory", dpt);
        map.put("RoleFactory", role);

    }

    public static void loadFactory() {
        UserInfoFactory user = getUserInfoFactory();
        user.load();
        if (user.isInitialized()) {
            getDepartmentFactory().load();
            getRoleFactory().load();
        } else {
            logger.fatal("error connect UAM data , system can not login!!!");

        }
    }

    public static void putUAMRequest(AbstractUAMRequest uamRequest) {
        map.put("UAMRequest", uamRequest);
    }

    /**
     * 动态获取UAM工厂类
     * 
     * @param classname
     * @return
     */
    public static Object get(String classname) {
        return map.get(classname);
    }

    /**
     * 获取用户工厂类
     * 
     * @return
     */
    public static UserInfoFactory getUserInfoFactory() {
        return (UserInfoFactory) map.get("UserInfoFactory");
    }

    public static DepartmentFactory getDepartmentFactory() {
        return (DepartmentFactory) map.get("DepartmentFactory");
    }

    public static RoleFactory getRoleFactory() {
        return (RoleFactory) map.get("RoleFactory");
    }

    public static AbstractUAMRequest getUAMRequest() {
        return (AbstractUAMRequest) map.get("UAMRequest");
    }

}
